<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
	<title>Document</title>
	<link rel="stylesheet" href="./bootstrap/css/bootstrap.css">
	<link rel="stylesheet" href="./css/base.css">
	<link href="https://cdn.bootcdn.net/ajax/libs/lightbox2/2.9.0/css/lightbox.min.css" rel="stylesheet">
	<style>
		.content .item {

			margin-top: 10px;
			margin-bottom: 10px;
		}

		.content .item img {
			width: 100%;
			height: 100%;
			box-shadow: 0px 0px 10px #ccc;

		}

		.content .item .title:hover {
			text-decoration: underline;
			cursor: pointer;

		}


		.content .item div:first-child:hover img {
			opacity: 0.7;
			cursor: pointer;
			transition: all 0.3s ease-in-out;
		}

		.content .item .intro {
			margin-top: 10px;
			color: #ccc;
			font-size: 14px;
		}
	</style>
</head>

<body>
	<div class="container">
		<nav class="navbar navbar-default">
			<div class="container-fluid">
				<!-- Brand and toggle get grouped for better mobile display -->
				<div class="navbar-header">
					<button type="button" class="navbar-toggle collapsed" data-toggle="collapse"
						data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
						<span class="sr-only">Toggle navigation</span>
						<span class="icon-bar"></span>
						<span class="icon-bar"></span>
						<span class="icon-bar"></span>
					</button>
					<a class="navbar-brand" href="#" style="display:flex;">
						汪玮的博客
					</a>
				</div>

				<!-- Collect the nav links, forms, and other content for toggling -->
				<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">

					<ul class="nav navbar-nav navbar-right">
						<li><a href="#">首页</a></li>
						<li class="dropdown">
							<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button"
								aria-haspopup="true" aria-expanded="false">分类 <span class="caret"></span></a>
							<ul class="dropdown-menu" id="cates">
								<!-- <li><a href="/cate.html?id=2">html</a></li>
									<li><a href="/cate.html?id=2">css</a></li>
									<li><a href="/cate.html?id=2">javascript</a></li>
									<li role="separator" class="divider"></li>
									<li><a href="/cate.html?id=2">mysql</a></li>
									<li><a href="/cate.html?id=2">Linux</a></li> -->
							</ul>
						</li>
						<li><a href="/tags.html">标签</a></li>
						<li><a href="/about.html">关于我</a></li>
					</ul>
				</div><!-- /.navbar-collapse -->
			</div><!-- /.container-fluid -->
		</nav>

		<div class="content">
			<h4>最新文章</h4>
			<div class="artlist" id="Txts">
			</div>
		</div>
		<button type="button" class="btn  btn-block" id="loadMore">查看更多</button>
		<hr>

		<div class="footer" style="text-align:center;font-size: 14px; color: #999;margin:10px 0;">
			首页 | 主站 | GitHub | created at 2020/02/4 | Powered by 汪玮
		</div>
	</div>
</body>
<script src="./js/jquery.js"></script>
<script src="./bootstrap/js/bootstrap.min.js"></script>
<script src="https://momentjs.bootcss.com/downloads/moment.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/lightbox2/2.9.0/js/lightbox-plus-jquery.min.js"></script>

<script>
	let page = 1;
	let pagesize = 3;
	let isFinish = false;
	let url = location.href
	let reg = /\?id=(.+)\&?/
	let [, id] = Array.from(url.match(reg))

	// 数据请求
	function loadTxt(page, pagesize, id) {
		console.log(page,pagesize,id);
		return $.ajax({
			url: `http://127.0.0.1:8888/api/catelist`,
			data: {
				page,
				pagesize,
				id
			}
		})
	}
	function loadCate() {
		return $.ajax({
			url: 'http://127.0.0.1:8888/api/cate',
		})
	}

	//初始化数据
	async function initData() {
		//加载文章数据和分类数据
		let pro2 = loadCate();
		let pro3 = loadTxt(page, pagesize, id);
		let data = await Promise.all([pro2, pro3]);
		let [cates, Txts] = data;
		//渲染所有的分类数据
		let lis = renderCate(cates);
		let articleData = renderTxts(Txts);
		$("#cates").html(lis)
		$("#Txts").html(articleData)
	}

	//渲染分类数据
	function renderCate(cates) {
		let lis = '';
		cates.forEach((cat) => {
			let { cate_id, cate_name } = cat;
			//拼接字符串
			lis += `<li><a href="/cate.html?id=${cate_id}">${cate_name}</a></li>`
		});
		return lis;
	}
	//渲染文章数据
	function renderTxts(Txts) {
		let divs = '';
		Txts.forEach((art) => {
			let { cat_id, cate_name, content, is_show, img, author, addtime, article } = art;
			if (!img) {
				img || (img = 'http://placehold.it/200x100/bluesky/aaabbb?text=MingDe')
			} else {
				img = 'http://127.0.0.1:8888/' + img;

			}
			//拼接字符串
			addtime = moment(addtime).format('MMMM Do YYYY, h:mm:ss a');
			divs += `<div class='item row'>
					<div class="col-md-2" witdh=200px>
						<a href="${img}" data-lightbox="preview" data-title="${author}">
							<img src="${img}" alt="">
						</a>
					</div>
					<div class="col-md-10">
						<div class="date ">作者：${author} 、${addtime} 、分类：${cate_name}</div>
						<div class="title">${article}</div>
					</div>
					<hr>
				</div>`
		});
		return divs;
	}

	initData();
	//加载更多
	$("#loadMore").on('click', function () {
		console.log('点击了');
		$(this).prop('disabled', true).html('loding...');
		page++;
		loadTxt(page,pagesize,id).then(res => {
			if (isFinish) {
			layer.msg('数据已经加载完毕', { icon: 0 })
			return;
		}
			$(this).prop('disabled', false).html('查看更多');
			let articlesHtml = renderTxts(res)
			let oldHtml = $("#Txts").html();
			$("#Txts").html(oldHtml + articlesHtml);
		})
	})
</script>

</html>